﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using Gift.Model.ENUM;

namespace WebSite.admin.title
{
    public partial class addnews : BaseAdmin
    {
        // Fields
     
        public string times = "";
  

        // Methods
        protected void Button1_Click(object sender, EventArgs e)
        {
            upload upload = new upload();
            string str = upload.Up(this.file1, "../../uploadfile/news/");
            base.Response.Write(upload.Resup[Convert.ToInt32(str)]);
            this.newsfile.Text = upload.s;
        }

        private void Display(string parentid, string space)
        {
            string sql = "Select * From DB_Newsclass Where parentid =" + parentid + " and languages=" + info.languages("") + " Order By classorder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.typeid.Items.Add(new ListItem(space + "" + row["classname"].ToString() + "", "" + row["classid"].ToString() + ""));
                    this.Display(row["classid"].ToString(), "|-" + space);
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string str2;
            if (this.typeid.SelectedItem.Value.ToString() != "0")
            {
                str2 = this.typeid.SelectedItem.Value.ToString();
            }
            else
            {
                str2 = "0";
            }
            if (base.Request.QueryString["nid"] != null)
            {
                SQLAccess.DoSql(string.Concat(new object[] { 
                "update DB_News set typeid=", str2, ",newsname='", re.replace(this.newsname.Value), "',newsorder=", re.replace(this.newsorder.Value), ",author='", re.replace(this.author.Value), "',email='", re.replace(this.email.Value), "',visible=", this.visible.Checked ? "1" : "0", ",isnew=", this.isnew.Checked ? "1" : "0", ",keys='", re.replace(this.keys.Value), 
                "',updatetime='", re.replace(base.Request.Form["updatetime"]), "',newsurl='", re.replace(this.newsurl.Value), "',newsfile='", re.replace(this.newsfile.Text), "',newsfileurl='", re.replace(this.newsfileurl.Value), "',ishot=", re.replace(this.ishot.SelectedValue), " where newsid=", base.Request.QueryString["nid"], ""
             }));
                base.Response.Write("<script language='javascript'>alert('文章信息修改成功');window.location.href='addnews.aspx?nid=" + base.Request.QueryString["nid"] + "';</script>");
                base.Response.End();
            }
            else
            {
                SQLAccess.DoSql(string.Concat(new object[] { 
                "insert into DB_News (typeid,newsname,newsorder,visible,author,rootin,updatetime,isnew,email,keys,newsurl,newsfile,newsfileurl,ishot,languages) values (", str2, ",'", re.replace(this.newsname.Value), "',", re.replace(this.newsorder.Value), ",", this.visible.Checked ? "1" : "0", ",'", re.replace(this.author.Value), "','','", re.replace(base.Request.Form["updatetime"]), "',", this.isnew.Checked ? "1" : "0", ",'", re.replace(this.email.Value), 
                "','", re.replace(this.keys.Value), "','", re.replace(this.newsurl.Value), "','", re.replace(this.newsfile.Text), "','", re.replace(this.newsfileurl.Value), "',", re.replace(this.ishot.SelectedValue), ",", info.languages(""), ")"
             }));
                string sql = "select top 1 * from DB_News order by newsid desc";
                DataTable table = SQLAccess.GreatDs(sql).Tables[0];
                if (table.Rows.Count > 0)
                {
                    base.Response.Write("<script language='javascript'>alert('文章信息添加成功');window.location.href='addnews02.aspx?nid=" + table.Rows[0]["newsid"].ToString() + "';</script>");
                    base.Response.End();
                }
                else
                {
                    base.Response.Write("<script language='javascript'>alert('文章信息添加成功');window.location.href='news.aspx';</script>");
                    base.Response.End();
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            this.ImageButton1.Attributes["onclick"] = "return CheckFrm()";
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.AddArticle);
                this.times = DateTime.Now.ToString();
                this.Display("0", "|--");
                if (base.Request.QueryString["nid"] != null)
                {
                    this.upbind();
                }
            }
        }

        private void upbind()
        {
            string sql = "select top 1 * from DB_News where newsid=" + base.Request.QueryString["nid"];
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.typeid.SelectedValue = table.Rows[0]["typeid"].ToString();
            this.visible.Checked = (bool)table.Rows[0]["visible"];
            this.newsname.Value = table.Rows[0]["newsname"].ToString();
            this.ishot.SelectedValue = table.Rows[0]["ishot"].ToString();
            this.author.Value = table.Rows[0]["author"].ToString();
            this.email.Value = table.Rows[0]["email"].ToString();
            this.keys.Value = table.Rows[0]["keys"].ToString();
            this.newsurl.Value = table.Rows[0]["newsurl"].ToString();
            this.newsfile.Text = table.Rows[0]["newsfile"].ToString();
            this.newsfileurl.Value = table.Rows[0]["newsfileurl"].ToString();
            this.isnew.Checked = (bool)table.Rows[0]["isnew"];
            this.newsorder.Value = table.Rows[0]["newsorder"].ToString();
            this.times = table.Rows[0]["updatetime"].ToString();
        }

        // Properties
        protected global_asax ApplicationInstance
        {
            get
            {
                return (global_asax)this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }

    }
}